package com.ruoyi.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class SqlUtils {

    public static List<String> getInsertRsA17Sql_hb(List<Map<String, Object>> listmap, String sid, String pch, boolean isNeedNewestRec) {
        List<String> list = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        list.add("delete from sys_user_resume where sid='" + sid + "' AND pch='" + pch + "'");
        String recordId = "";
        int max = 0;
        String uuid = "";
        Map<String, Object> map = null;
        for (int i = 0; i < listmap.size(); i++) {
            map = listmap.get(i);
            uuid = UUID.randomUUID().toString();
            String sql = "insert into sys_user_resume(id,sid,pch,start_time,end_time,duities) values('" + uuid + "','" + sid + "','" + pch + "',";
            String key = StringUtils.getString(map.get("key"));
            String value = StringUtils.getString(map.get("value"));
            String val = StringUtils.getString(map.get("value"));
            if (!StringUtils.isBlank(key) || !StringUtils.isBlank(value)) {



                if (i == listmap.size() - 1 && StringUtils.isBlank(key) ) {
                    sql = sql + "null,null,'" + value + "')";

                    list.add(sql);

                    sb.append("\r                  " + val + "\r");
                    break;
                }
                if (StringUtils.isNotBlank(key) && key.indexOf("--") != -1) {
                    String[] arg = key.split("--");
                    if (StringUtils.isNotBlank(arg[0]) && ResumeUtil.checkDateString(arg[0]).isSuccess()) {
                        int temp = Integer.parseInt(arg[0].replace(".", ""));
                        if (temp > max) {
                            max = temp;
                            recordId = uuid;
                        }
                        sql = sql + "'" + arg[0].replace(".", "") + "',";
                    } else {
                        throw new RuntimeException("该简历中”" + key + "“的格式有问题！");
                    }
                    if (arg.length >= 2 && StringUtils.isNotBlank(arg[1])) {
                        Result res = ResumeUtil.checkDateString(arg[1]);
                        if (res.isSuccess()) {
                            sql = sql + "'" + arg[1].replace(".", "") + "',";
                        } else {
                            throw new RuntimeException(res.getMsg());
                        }
                    } else {
                        sql = sql + "null,";
                    }
                } else {
                    throw new RuntimeException("该简历中的“" + key + "”时间格式有问题！");
                }
                if (StringUtils.isNotBlank(value)) {
                    value = value.replace("<br/>", "").replace("</br>", "");
                    sql = sql + "'" + value + "'";
                    val = val.replace("<br/>", "\n                  ").replace("</br>", "\n                  ");
                } else {
                    throw new RuntimeException("该简历中“" + key + "”后边没有职务信息！");
                }
                sql = sql + ")";
                list.add(sql);

                sb.append(key + "  " + val + "\r");
            }
        }  if (StringUtils.isNotBlank(sb.toString())) {
            sb.deleteCharAt(sb.length() - 2);
        }
        list.add(sb.toString());
        if (isNeedNewestRec) {
            list.add(recordId);
        }
        return list;
    }



    public static List<String> getInsertRsA17Sql2(List<Map<String, String>> listmap, String sid, boolean isNeedNewestRec) {
        List<String> list = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        list.add("delete from rs_a17 where sid='" + sid + "'");
        String recordId = "";
        int max = 0;
        String uuid = "";
        Map<String, String> map = null;
        for (int i = 0; i < listmap.size(); i++) {
            map = listmap.get(i);
            uuid = UUID.randomUUID().toString();
            String sql = "insert into rs_a17(id,sid,a1905,a1910,a1920) values('" + uuid + "','" + sid + "',";
            String key = map.get("key");
            String value = map.get("value");
            String val = map.get("value");
            if (!StringUtils.isBlank(key) || !StringUtils.isBlank(value)) {



                if (i == listmap.size() - 1 && StringUtils.isBlank(key) && ResumeUtil.isHaveString(value)) {
                    sql = sql + "null,null,'" + value + "')";

                    list.add(sql);

                    sb.append("                  " + val + "\r");
                    break;
                }
                if (StringUtils.isNotBlank(key) && key.indexOf("--") != -1) {
                    String[] arg = key.split("--");
                    if (StringUtils.isNotBlank(arg[0]) && ResumeUtil.checkDateString(arg[0]).isSuccess()) {
                        int temp = Integer.parseInt(arg[0].replace(".", ""));
                        if (temp > max) {
                            max = temp;
                            recordId = uuid;
                        }
                        sql = sql + "'" + arg[0].replace(".", "") + "',";
                    } else {
                        throw new RuntimeException("该简历中”" + key + "“的格式有问题！");
                    }
                    if (arg.length >= 2 && StringUtils.isNotBlank(arg[1])) {
                        Result res = ResumeUtil.checkDateString(arg[1]);
                        if (res.isSuccess()) {
                            sql = sql + "'" + arg[1].replace(".", "") + "',";
                        } else {
                            throw new RuntimeException(res.getMsg());
                        }
                    } else {
                        sql = sql + "null,";
                    }
                } else {
                    throw new RuntimeException("该简历中的“" + key + "”时间格式有问题！");
                }
                if (StringUtils.isNotBlank(value)) {
                    value = value.replace("<br/>", "").replace("</br>", "");
                    sql = sql + "'" + value + "'";
                    val = val.replace("<br/>", "\n                  ").replace("</br>", "\n                  ");
                } else {
                    throw new RuntimeException("该简历中“" + key + "”后边没有职务信息！");
                }
                sql = sql + ")";
                list.add(sql);

                sb.append(key + "  " + val + "\r");
            }
        }  if (StringUtils.isNotBlank(sb.toString())) {
            sb.deleteCharAt(sb.length() - 2);
        }
        list.add(sb.toString());
        if (isNeedNewestRec) {
            list.add(recordId);
        }
        return list;
    }



    public static Result getInsertRsA17Sql(List<Map<String, Object>> listmap, String sid, boolean isNeedNewestRec) {
        List<String> list = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        list.add("delete from sys_user_resume_big where sid='" + sid + "'");
        String recordId = "";
        int max = 0;
        String uuid = "";
        Map<String, Object> map = null;
        for (int i = 0; i < listmap.size(); i++) {
            map = listmap.get(i);
            uuid = UUID.randomUUID().toString();
            String sql = "insert into sys_user_resume_big(id,sid,a1905,a1910,a1920,a1950) values('" + uuid + "','" + sid + "',";
            String key = (String)map.get("key");
            String value = (String)map.get("value");
            String val = (String)map.get("value");
            String A1905 = "";
            String A1910 = "";
            String A1950 = "";
            if (!StringUtils.isBlank(key) || !StringUtils.isBlank(value)) {



                if (i == listmap.size() - 1 && StringUtils.isBlank(key)) {
                    sql = sql + "null,null,'" + value + "',null)";

                    list.add(sql);

                    sb.append("\r                  " + val + "\r");
                    break;
                }
                if (StringUtils.isNotBlank(key) && key.indexOf("--") != -1) {
                    String[] arg = key.split("--");
                    if (StringUtils.isNotBlank(arg[0]) && ResumeUtil.checkDateString(arg[0]).isSuccess()) {
                        int temp = Integer.parseInt(arg[0].replace(".", ""));
                        if (temp > max) {
                            max = temp;
                            recordId = uuid;
                        }
                        sql = sql + "'" + arg[0].replace(".", "") + "',";
                        A1905 = arg[0].replace(".", "");
                    } else {
                        return Result.fail("该简历中”" + key + "“的格式有问题！");
                    }
                    if (arg.length >= 2 && StringUtils.isNotBlank(arg[1])) {
                        Result res = ResumeUtil.checkDateString(arg[1]);
                        if (res.isSuccess()) {
                            sql = sql + "'" + arg[1].replace(".", "") + "',";
                            A1910 = arg[1].replace(".", "");
                        } else {
                            return Result.fail(res.getMsg());
                        }
                    } else {
                        sql = sql + "null,";
                    }
                } else {
                    return Result.fail("该简历中的“" + key + "”时间格式有问题！");
                }
                if (StringUtils.isNotBlank(value)) {
                    value = value.replace("<br/>", "").replace("</br>", "");
                    sql = sql + "'" + value + "'";
                    val = val.replace("<br/>", "\n                  ").replace("</br>", "\n                  ");
                } else {
                    return Result.fail("该简历中“" + key + "”后边没有职务信息！");
                }
                A1950 = StringHelper.countJianliA1950(A1905, A1910);

                sql = sql + ",'" + A1950 + "')";
                list.add(sql);

                sb.append(key + "  " + val + "\r");
            }
        }  if (StringUtils.isNotBlank(sb.toString())) {
            sb.deleteCharAt(sb.length() - 2);
        }
        list.add(sb.toString());
        if (isNeedNewestRec) {
            list.add(recordId);
        }

        return Result.check(list);
    }
}
